/*
 * @Author: herw
 * @Date: 2020-11-16 10:06:58
 * @Last Modified by: herw
 * @Last Modified time: 2020-11-18 14:59:53
 */
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.name"
          clearable
          placeholder="输入名称搜索"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button
            v-permission="['admin', 'model:edit']"
            :disabled="!selectIndex"
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-upload"
            @click="bpmn"
          >模型设计
          </el-button>
          <el-button
            v-permission="['admin', 'model:add']"
            :disabled="!selectIndex"
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-upload"
            :loading="crud.loading"
            @click="deploy"
          >部署
          </el-button>
        </template>
      </crudOperation>
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      highlight-current-row
      style="width: 100%"
      @selection-change="handleCurrentChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="key" label="模型key" />
      <el-table-column prop="name" label="模型名称" />
      <el-table-column prop="category" label="模型分类" />
      <el-table-column prop="version" label="版本" />
      <el-table-column prop="deployed" label="是否发布">
        <template slot-scope="scope">
          <span>{{ scope.row.deployed ? '是' : '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-permission="['admin', 'app:edit', 'app:del']"
        label="操作"
        width="150px"
        align="center"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="800px"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="small"
        label-width="100px"
      >
        <el-form-item label="模型key" prop="key">
          <el-input
            v-model="form.key"
            :readonly="readonly"
            style="width: 670px"
            placeholder="模型key"
          />
        </el-form-item>
        <el-form-item label="模型名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 670px"
            placeholder="模型名称"
          />
        </el-form-item>
        <el-form-item label="模型分类" prop="category">
          <el-input
            v-model="form.category"
            style="width: 670px"
            placeholder="模型分类"
          />
        </el-form-item>
        <el-form-item label="模型描述" prop="description">
          <el-input
            v-model="form.description"
            style="width: 670px"
            placeholder="模型描述"
          />
        </el-form-item>
        <el-form-item label="租户ID" prop="tenantId">
          <el-input
            v-model="form.tenantId"
            style="width: 670px"
            placeholder="租户ID"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudModel from '@/api/flowable/model'

const defaultForm = { key: null, name: null, category: null, description: null, tenantId: null }
export default {
  name: 'Model',
  components: { pagination, crudOperation, rrOperation, udOperation },
  cruds() {
    return CRUD({ title: '模型', url: 'api/flowable/model', crudMethod: { ...crudModel }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      readonly: false,
      selectIndex: '',
      permission: {
        add: ['admin', 'model:add'],
        edit: ['admin', 'model:edit'],
        del: ['admin', 'model:del']
      },
      rules: {
        key: [
          { required: true, message: '请输入应用名称', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入应用名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 打开编辑弹窗前做的操作
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.readonly = true
    },
    handleCurrentChange(selection) {
      this.crud.selections = selection
      if (selection.length === 1) {
        const row = selection[0]
        this.selectIndex = row.id
        this.currentRow = row
      } else {
        this.currentRow = {}
        this.selectIndex = ''
      }
    },
    bpmn() {
      this.$router.push({ path: '/workflow/flowable/model/bpmn/' + this.selectIndex })
    },
    deploy() {
      this.crud.loading = true
      crudModel.deploy(this.currentRow)
        .then((res) => {
          this.crud.loading = false
          this.$notify({
            title: '部署成功',
            type: 'success',
            duration: 1500
          })
          this.crud.toQuery()
        })
        .catch(err => {
          this.crud.loading = false
          console.log(err.response.data.message)
        })
    }
  }
}
</script>
